//
//  Extensions.swift
//  KOLToken
//
//  Created by yangsan on 2020/2/26.
//  Copyright © 2020 iOS-team. All rights reserved.
//

import UIKit
import Foundation
import HandyJSON
import DynamicColor

//MARK: - UIViewController
private var key = "shouldNavigationBarHidden"
private var popKey = "popKey"
extension UIViewController {
    var shouldNavigationBarHidden: Bool {
        set {
            objc_setAssociatedObject(self, &key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
        get {
            guard let value = objc_getAssociatedObject(self, &key) else {
                return false
            }
            return value as! Bool
        }
    }
    
    /// 是否禁止滑动返回（默认都是不禁止）
    var forbidPop: Bool {
        set {
            objc_setAssociatedObject(self, &popKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
        get {
            guard let value = objc_getAssociatedObject(self, &popKey) else {
                return false
            }
            return value as! Bool
        }
    }
}

//MARK: - Dictionary
extension Dictionary {
    /// 字段转字符串
    public func toString() -> String {
        let data = try? JSONSerialization.data(withJSONObject: self, options: [])
        let str = String(data: data!, encoding: String.Encoding.utf8)
        return str!
    }
}


extension UIView {
    /// view切圆角
    ///
    /// - Parameters:
    ///   - conrners: 圆角位置
    ///   - radius: 圆角半径
    func addRectCorner(conrners: UIRectCorner , radius: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
}

//MARK: - 字符串
extension String {
    
    private func key() -> Array<UInt8> {
        return [0x58,0x7a,0x0f,0xb0,0xc9,0x1a,0xe0,0x61,0xe6,0x6a,0xdb,0xf2,0xec,0x56,0xf0,0xb8]
    }
    private func iv() -> String {
        return "APMAESEncryption"
    }
    
    /// 字符串转字典
    public func toDictionary() -> [String : Any]? {
        let data = self.data(using: String.Encoding.utf8)
        if let dict = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String : Any] {
            return dict
        }
        return nil
    }
}

